package blocos;
import java.util.*;

import blocos.BlockGUI;

public class BlockManager {
	public List<BlockGUI> blocks;
	public BlockListener listener;
	public static int length = 0;	
	
	public BlockManager(){
		blocks = new ArrayList<BlockGUI>();
		listener = new BlockListener(this);
	}
	public void addBlock (BlockGUI b){
		b.addMouseListener(listener);
		b.addMouseMotionListener(listener);
		this.blocks.add(b);
		b.repaint();
		b.revalidate();
		
	}
	
	public void removeBlock (BlockGUI b){
		b.setVisible(false);
		blocks.remove(b);
	}
	
	
	
	public BlockGUI selectedBlock(int x, int y){
		Iterator<BlockGUI> ListIterator = this.blocks.iterator();
		while (ListIterator.hasNext()==true )
		{
			BlockGUI block ;
			block = ListIterator.next();
		
			if(isInsideBlock(block,x,y)){
				
				return block;
				
			}
		}
		
		return null;
	}
	
	public boolean isInsideBlock(BlockGUI block, int x, int y){
		if((x+10>=block.x)&&(x<=block.x+block.width)&&(y+10>=block.y)&&(y<=block.y+block.height))
			return true;
		else
			return false;
	}
	
	public void checkAttach(BlockGUI b){
		Iterator<BlockGUI> ListIterator = this.blocks.iterator();
		BlockGUI toRemove = null;
		
		while (ListIterator.hasNext()==true )
		{
			BlockGUI block ;
			block = ListIterator.next();
			if(b!=block){
				if(block.isVisible()==false){
					toRemove = block;
							
				}
				if(b.isAttachedToBottomOf(block)){
					b.attachToBottomOf(block);
					block.attachToTopOf(b);
				}
				else if(b.isAttachedToLeftOf(block)){
					b.attachToLeftOf(block);
					block.attachToRightOf(b);
				}
				else if(b.isAttachedToRightOf(block)){
					b.attachToRightOf(block);
					block.attachToLeftOf(b);

				}
				else if(b.isAttachedToTopOf(block)){
					b.attachToTopOf(block);
					block.attachToBottomOf(b);
				}
			}
			
		}
		if(toRemove!=null){
			toRemove.detachAll();
			blocks.remove(toRemove);
			toRemove = null;
		}
	}
	
	
	public void checkCollision(BlockGUI b){
		Iterator<BlockGUI> ListIterator = this.blocks.iterator();
		while (ListIterator.hasNext()==true )
		{
			BlockGUI block ;
			block = ListIterator.next();
			if(b.containsBlock(block)){
				b.attachToRightOf(block);
			}
		}
	}
		public void removeAll(){
		Iterator<BlockGUI> ListIterator = this.blocks.iterator();
		while (ListIterator.hasNext()==true )
		{
			BlockGUI block ;
			block = ListIterator.next();
			block.setVisible(false);
		}
		blocks.removeAll(blocks);
	}
	public void updateBlocks(){
		Iterator<BlockGUI> ListIterator = this.blocks.iterator();
		BlockGUI block ;
		while (ListIterator.hasNext()==true )
		{			
			block = ListIterator.next();
			checkAttach(block);					
		}
	}
	
	public void toTheEnd(BlockGUI b){
		blocks.remove(b);
		blocks.add(b);		
	}
	
	public void updateBlockPositions ()
	{
		Iterator<BlockGUI> ListIterator = this.blocks.iterator();
		BlockGUI block ;
		while (ListIterator.hasNext()==true )
		{			
			block = ListIterator.next();
			block.updatePosition();					
		}
	
	}
	
}
